﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HoluxConvertorCore;

namespace HoluxConvertor
{
    public partial class MainForm : Form
    {
        private AboutBox About;
        private AppSettings AppSet;
        List<GpxLog> Logs;
        int logIndex;

        public MainForm()
        {
            InitializeComponent();
            this.backgroundWorker1.WorkerReportsProgress = true;
            this.backgroundWorker1.WorkerSupportsCancellation = false;

            Logs = new List<GpxLog>();
        }

        private void LoadFile(String FileName)
        {
            try
            {
                List<String> LogFile = new List<String>();

                AppSet.LoadDirectory = new FileInfo(FileName).Directory.Name;

                StreamReader Reader = new StreamReader(FileName);
                String Line = "";

                while (Line != null)
                {
                    Line = Reader.ReadLine();
                    LogFile.Add(Line);
                }

                this.toolStripStatusLabel.Text = "File loaded";

                Reader.Close();

                Logs.Add(new GpxLog(LogFile, FileName));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: File could not be read");
            }       
        }

        private void FilesLoadClick(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.InitialDirectory = AppSet.LoadDirectory;
            ofd.Filter = "gpx files|*.gpx|All files |*.*";
            ofd.FilterIndex = 1;
            ofd.RestoreDirectory = true;
            ofd.Multiselect = true;

            Logs.Clear();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                foreach (String Name in ofd.FileNames)
                {
                    LoadFile(Name);
                }
            }

            logIndex = 0; 
        }

        private void ActionsConvertClick(object sender, EventArgs e)
        {
            logIndex = 0;

            if (backgroundWorker1.IsBusy == false)
            {
                this.toolStripProgressBar.Visible = true;
                this.backgroundWorker1.RunWorkerAsync();
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            List<String> LogToSave;

            foreach (GpxLog Log in Logs)
            {
                LogToSave = Log.GpxLogConvert(ProgressBarHandle);

                using (StreamWriter sw = new StreamWriter(Log.Name.Insert(Log.Name.Length - 4, "_Converted")))
                {
                    foreach (String Line in LogToSave)
                    {
                        sw.WriteLine(Line);

                    }
                    sw.Close();
                }

                logIndex++;
            }
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.toolStripStatusLabel.Text = "In progress: " + e.ProgressPercentage + "%";
            this.toolStripProgressBar.Value = e.ProgressPercentage;

            if (e.ProgressPercentage == 100)
            {
                this.toolStripStatusLabel.Text = "Done";
                this.toolStripProgressBar.Value = 100;
                this.toolStripProgressBar.Visible = false;
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About = new AboutBox();
            About.Show();
        }

        private void MainFormLoad(object sender, EventArgs e)
        {
            AppSet = new AppSettings();

            String[] arguments = Environment.GetCommandLineArgs();

            if (arguments.Length > 1)
            {
                if (Path.GetExtension(arguments[1]) == ".gpx")
                {
                    this.LoadFile(arguments[1]);
                }
            }

            logIndex = 0;
        }

        private void MainFormClosing(object sender, FormClosedEventArgs e)
        {
            AppSet.Save();
        }

        private void ProgressBarHandle(int Perc)
        {
            if ((Logs.Count == logIndex + 1) && (Perc == 100))
            {
                this.backgroundWorker1.ReportProgress(100);
            }
            else 
            {
                this.backgroundWorker1.ReportProgress((Perc / Logs.Count) + ((logIndex * 100) / Logs.Count));
            }
        }
    }
}
